package weeny.recycle;

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.JTextField;
import javax.swing.SpinnerModel;
import javax.swing.SpinnerNumberModel;

/**
 * 
 * DEAD
 * 
 * @author weeny
 *
 */
@Deprecated
public class LabelSpinner extends JPanel{
	private JLabel label;
	private JSpinner spinner;
	public LabelSpinner() {
		this("label");
	}
	public LabelSpinner(String label){
		setLayout(new FlowLayout(FlowLayout.RIGHT));
		spinner = new JSpinner();
		spinner.setPreferredSize(new Dimension(65,24));
		this.setBorder(null);
		this.label = new JLabel(label);
		this.add(this.label);
		this.add(spinner);
		this.setPreferredSize(new Dimension(200,30));
	}
	@Override
	public void setEnabled(boolean enabled) {
		super.setEnabled(enabled);
		for(int i = 0; i<getComponentCount(); i++){
			getComponent(i).setEnabled(enabled);
		}
	}
	public void register(SpinnerModel model, final SetRunOne run, double dft){
		register(spinner, model, run, dft);
	}
	public void register(SpinnerModel model, final SetRunOne run, int dft){
		register(spinner, model, run, dft);
	}
	public void setSpinWidth(int width){
		spinner.setPreferredSize(new Dimension(width, 24));
	}
	public void register(final SetRunOne run, double dft, double min, double max, double step){
		register(label.getText(), run, dft, min, max, step);
	}
	public void register(final SetRunOne run, int dft, int min, int max, int step){
		register(label.getText(), run, dft, min, max, step);
	}
	public void register(String label, final SetRunOne run, int dft, int min, int max, int step){
		this.label.setText(label);
		register(spinner, run, dft, min, max, step);
	}
	public void register(String label, SetRunOne run, double dft, double min, double max, double step){
		this.label.setText(label);
		register(spinner, run, dft, min, max, step);
	}
	
	public static void register(final JSpinner spinner, final SpinnerModel model, final SetRunOne run, double dft){
		spinner.setModel(model);
		final MouseAdapter mouse = new MouseAdapter(){
			@Override
			public void mouseReleased(MouseEvent e) {
				JTextField field = (JTextField) spinner.getEditor().getComponent(0);
				try{
					double val = Double.parseDouble(field.getText());
					if (model instanceof SpinnerNumberModel) {
						SpinnerNumberModel new_name = (SpinnerNumberModel) model;
						if (val > (Double) new_name.getMaximum()) {
							field.setText(""+new_name.getMaximum());
							spinner.setValue(new_name.getMaximum());
							val = (Double) new_name.getMaximum();
						}
						else if (val < (Double) new_name.getMinimum()) {
							field.setText(""+new_name.getMinimum());
							spinner.setValue(new_name.getMinimum());
							val = (Double) new_name.getMinimum();
						}
					}
					run.set(val);
				}catch(NumberFormatException exp){
					field.setText(spinner.getValue().toString());
					spinner.setValue(spinner.getValue());
				}
				
			}
		};
		KeyAdapter key = new KeyAdapter(){
			@Override
			public void keyReleased(KeyEvent e) {
				System.out.println("key stroked");
				if (e.getKeyCode() == '\n') {//is return
					mouse.mouseReleased(null);
				}
			}
		};
		spinner.getComponent(0).addMouseListener(mouse);
		spinner.getComponent(1).addMouseListener(mouse);
		spinner.getEditor().getComponent(0).addKeyListener(key);
	}
	public static void register(final JSpinner spinner, final SpinnerModel model, final SetRunOne run, int dft){
		spinner.setModel(model);
		final MouseAdapter mouse = new MouseAdapter(){
			@Override
			public void mouseReleased(MouseEvent e) {
				JTextField field = (JTextField) spinner.getEditor().getComponent(0);
				try{
					double val = Double.parseDouble(field.getText());
					int num = (int) val;
					if (model instanceof SpinnerNumberModel) {
						SpinnerNumberModel new_name = (SpinnerNumberModel) model;
						if (num > (Integer) new_name.getMaximum()) {
							num = (Integer) new_name.getMaximum();
						}
						else if (num < (Integer) new_name.getMinimum()) {
							num = (Integer) new_name.getMinimum();
						}
					}
					field.setText(""+num);
					spinner.setValue(num);
					run.set(num);
				}catch(NumberFormatException exp){
					field.setText(spinner.getValue().toString());
					spinner.setValue(spinner.getValue());
				}
				
			}
		};
		KeyAdapter key = new KeyAdapter(){
			@Override
			public void keyReleased(KeyEvent e) {
				if (e.getKeyCode() == '\n') {
					mouse.mouseReleased(null);
				}
			}
		};
		spinner.getComponent(0).addMouseListener(mouse);
		spinner.getComponent(1).addMouseListener(mouse);
		spinner.getEditor().getComponent(0).addKeyListener(key);
	}
	public static void register(final JSpinner spinner, final SetRunOne run, int dft, int min, int max, int step){
		register(spinner, new SpinnerNumberModel(dft, min,max, step), run, dft);
	}
	public static void register(final JSpinner spinner, final SetRunOne run, double dft, double min, double max, double step){
		register(spinner, new SpinnerNumberModel(dft, min,max, step), run, dft);
	}
}
